ifaceeface 都是 Go 中描述接口的底层结构体,区别在于 iface 描述的接口包含方法,而 eface 则是不包含任何方法的空接口:interface{}

从源码层面看一下:

  1. type iface struct {
  2. tab *itab
  3. data unsafe.Pointer
  4. }
  5. type itab struct {
  6. inter *interfacetype
  7. _type *_type
  8. link *itab
  9. hash uint32 // copy of _type.hash. Used for type switches.
  10. bad bool // type does not implement interface
  11. inhash bool // has this itab been added to hash?
  12. unused [2]byte
  13. fun [1]uintptr // variable sized
  14. }

iface 内部维护两个指针,tab 指向一个 itab 实体, 它表示接口的类型以及赋给这个接口的实体类型。data 则指向接口具体的值,一般而言是一个指向堆内存的指针。

再来仔细看一下 itab 结构体:_type 字段描述了实体的类型,包括内存对齐方式,大小等;inter 字段则描述了接口的类型。fun 字段放置和接口方法对应的具体数据类型的方法地址,实现接口调用方法的动态分派,一般在每次给接口赋值发生转换时会更新此表,或者直接拿缓存的 itab。

这里只会列出实体类型和接口相关的方法,实体类型的其他方法并不会出现在这里。如果你学过 C++ 的话,这里可以类比虚函数的概念。

另外,你可能会觉得奇怪,为什么 fun 数组的大小为 1,要是接口定义了多个方法可怎么办?实际上,这里存储的是第一个方法的函数指针,如果有更多的方法,在它之后的内存空间里继续存储。从汇编角度来看,通过增加地址就能获取到这些函数指针,没什么影响。顺便提一句,这些方法是按照函数名称的字典序进行排列的。

再看一下 interfacetype 类型,它描述的是接口的类型:

  1. type interfacetype struct {
  2. typ _type
  3. pkgpath name
  4. mhdr []imethod
  5. }

可以看到,它包装了 _type 类型,_type 实际上是描述 Go 语言中各种数据类型的结构体。我们注意到,这里还包含一个 mhdr 字段,表示接口所定义的函数列表, pkgpath 记录定义了接口的包名。

这里通过一张图来看下 iface 结构体的全貌:

iface 结构体全景

接着来看一下 eface 的源码:

  1. type eface struct {
  2. _type *_type
  3. data unsafe.Pointer
  4. }

相比 ifaceeface 就比较简单了。只维护了一个 _type 字段,表示空接口所承载的具体的实体类型。data 描述了具体的值。

eface 结构体全景

我们来看个例子:

  1. package main
  2. import "fmt"
  3. func main() {
  4. x := 200
  5. var any interface{} = x
  6. fmt.Println(any)
  7. g := Gopher{"Go"}
  8. var c coder = g
  9. fmt.Println(c)
  10. }
  11. type coder interface {
  12. code()
  13. debug()
  14. }
  15. type Gopher struct {
  16. language string
  17. }
  18. func (p Gopher) code() {
  19. fmt.Printf("I am coding %s language\n", p.language)
  20. }
  21. func (p Gopher) debug() {
  22. fmt.Printf("I am debuging %s language\n", p.language)
  23. }

执行命令,打印出汇编语言:

  1. go tool compile -S ./src/main.go

可以看到,main 函数里调用了两个函数:

  1. func convT2E64(t *_type, elem unsafe.Pointer) (e eface)
  2. func convT2I(tab *itab, elem unsafe.Pointer) (i iface)

上面两个函数的参数和 ifaceeface 结构体的字段是可以联系起来的:两个函数都是将参数组装一下,形成最终的接口。

作为补充,我们最后再来看下 _type 结构体:

  1. type _type struct {
  2. // 类型大小
  3. size uintptr
  4. ptrdata uintptr
  5. // 类型的 hash 值
  6. hash uint32
  7. // 类型的 flag,和反射相关
  8. tflag tflag
  9. // 内存对齐相关
  10. align uint8
  11. fieldalign uint8
  12. // 类型的编号,有bool, slice, struct 等等等等
  13. kind uint8
  14. alg *typeAlg
  15. // gc 相关
  16. gcdata *byte
  17. str nameOff
  18. ptrToThis typeOff
  19. }

Go 语言各种数据类型都是在 _type 字段的基础上,增加一些额外的字段来进行管理的:

  1. type arraytype struct {
  2. typ _type
  3. elem *_type
  4. slice *_type
  5. len uintptr
  6. }
  7. type chantype struct {
  8. typ _type
  9. elem *_type
  10. dir uintptr
  11. }
  12. type slicetype struct {
  13. typ _type
  14. elem *_type
  15. }
  16. type structtype struct {
  17. typ _type
  18. pkgPath name
  19. fields []structfield
  20. }

这些数据类型的结构体定义,是反射实现的基础。

参考资料

【有汇编分析,不错】http://legendtkl.com/2017/07/01/golang-interface-implement/

【interface 源码解读 很不错 包含反射】http://wudaijun.com/2018/01/go-interface-implement/